﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Activities;
using Microsoft.QualityTools.Testing.Fakes;
using HybridLab.Virtual.UnitTests.Utils;
using HybridLab.Workflow.Activities;
using HybridLab.Workflow.Activities.Info;
using System.Collections.Generic;
using HybridLab.Virtual.Interfaces.Fakes;

namespace HybridLab.Virtual.UnitTests
{
	[TestClass]
	public class TestCreateHybridLabSnapshot
	{
		[TestMethod]
		public void TestCreateHybridLabSnapshotActivity()
		{
			using (var context = ShimsContext.Create())
			{
				var testWorkflow = new TestCreateHybridLabSnapshotWorkflow();
				var workflowInvoker = new WorkflowInvoker(testWorkflow);

				var snapshots = TestUtil.GetFakeSnapshots();
				var appliedSnapshots = new Dictionary<string, string>();
				ShimVirtualHostCatalog.GetHostStringStringStringStringString = (t, h, u, d, p) =>
				{
					return TestUtil.GetFakeHost(snapshots, appliedSnapshots);
				};

				// set the args with fakes
				var details = new HybridLabWorkflowDetails()
					{
						HybridLab = new HybridLabEnvironment()
						{
							HostType = "HyperV",
							HostName = "localhost",
							Credentials = new HybridLabHostCredentials() { UserName = "bob", Password = "pass" },
							EnvironmentSnapshotInfo = new HybridLabEnvironmentSnapshotInfo()
							{
								Snapshots = snapshots.Keys.ToList().Select(k => new HybridLabVMSnapshotInfo() { MachineName = k }).ToList()
							}
						},
						DeploymentDetails = TestUtil.GetFakeDeploymentDetails()
					};
				testWorkflow.HybridLabWorkflowDetails = new InArgument<HybridLabWorkflowDetails>(c => details);
				
				// run the workflow
				var result = workflowInvoker.Invoke();

				// check that the snapshots were created
				Assert.AreEqual(0, appliedSnapshots.Count, "Applied snapshots");
				Assert.AreEqual(2, snapshots.Count, "Snapshot count");
				Assert.AreEqual(2, snapshots["TestMachine1"].Count, "TestMachine1 snapshot count");
				Assert.AreEqual(2, snapshots["TestMachine2"].Count, "TestMachine2 snapshot count");
				CollectionAssert.Contains(snapshots["TestMachine1"], "Test", "TestMachine1 snapshot missing");
				CollectionAssert.Contains(snapshots["TestMachine2"], "Test", "TestMachine2 snapshot missing");
			}
		}
	}
}
